package com.eedi.framework.template.dal.mapper;

import cn.hutool.core.util.StrUtil;
import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.enums.UserTypeEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.mybatis.core.dataobject.BaseDO;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.framework.template.controller.sys.vo.role.SysTemplateRolePageReq;
import com.eedi.framework.template.controller.sys.vo.role.SysTemplateRoleSimpleResp;
import com.eedi.framework.template.convert.SysTemplateRoleConvert;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.lang.Nullable;
import com.eedi.framework.template.dal.dataobject.SysTemplateRoleDO;
import java.util.Collection;
import java.util.List;

@Mapper
public interface SysTemplateRoleMapper extends BaseMapperX<SysTemplateRoleDO> {


    default SysTemplateRoleDO selectByName(String name, UserTypeEnum systemUserType) {
        LambdaQueryWrapperX<SysTemplateRoleDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(SysTemplateRoleDO::getSysRoleName, name);
        return selectOne(queryWrapper);
    }

    default SysTemplateRoleDO selectByCode(String code, UserTypeEnum systemUserType) {
        LambdaQueryWrapperX<SysTemplateRoleDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(SysTemplateRoleDO::getSysRoleCode, code);
        return selectOne(queryWrapper);
    }

    default List<SysTemplateRoleDO> selectListByStatus(@Nullable Collection<CommonStatusEnum> statuses) {
        LambdaQueryWrapperX<SysTemplateRoleDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.inIfPresent(SysTemplateRoleDO::getSysRoleStatus, statuses);
        return selectList(queryWrapper);
    }

    default List<SysTemplateRoleSimpleResp> selectByRoleIds(Collection<String> roleIds){
        List<SysTemplateRoleDO> sysRoleDOS = selectBatchIds(roleIds);
        return sysRoleDOS.stream().map(SysTemplateRoleConvert.INSTANCE::convert).toList();
    }

    default List<SysTemplateRoleSimpleResp> getByCode(String code){
        if (StrUtil.isBlank(code)){
            return List.of();
        }
        List<SysTemplateRoleDO> sysRoles = selectList(SysTemplateRoleDO::getSysRoleCode, code);
        return sysRoles.stream().map(SysTemplateRoleConvert.INSTANCE::convert).toList();
    }
    default PageResult<SysTemplateRoleDO> selectPage(SysTemplateRolePageReq  reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SysTemplateRoleDO>()
                .likeIfPresent(SysTemplateRoleDO::getSysRoleName, reqVO.getSysRoleName())
                .likeIfPresent(SysTemplateRoleDO::getSysRoleCode, reqVO.getSysRoleCode())
                .eqIfPresent(SysTemplateRoleDO::getSysRoleStatus, reqVO.getSysRoleStatus())
                .betweenIfPresent(BaseDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(SysTemplateRoleDO::getId));
    }



}
